package top.minuy.structure.sqrt;

import top.minuy.util.generate.ArrayGenerator;
import top.minuy.util.operation.Mergers;

/**
 * @author Minuy
 * @time 2:25
 * @date 2021/11/8
 */
public class Test {
    static class MMax implements Mergers<Integer>{

        /**
         * 合并数组
         *
         * @param array 要处理的数组
         * @return E
         * @author Minuy
         * @date 2021/11/10 22:16
         */
        @Override
        public Integer merges(Integer[] array) {
            Integer max = array[0];
            for (Integer i:array){
                max = max>i?max:i;
            }
            return max;
        }
    }

    static class MAdd implements Mergers<Integer>{

        /**
         * 合并数组
         *
         * @param array 要处理的数组
         * @return E
         * @author Minuy
         * @date 2021/11/10 22:16
         */
        @Override
        public Integer merges(Integer[] array) {
            Integer sum = 0;
            for (Integer i:array){
                sum+=i;
            }
            return sum;
        }
    }
    public static void main(String[] args) {
        testType(100,new MAdd());
        testType(98,new MAdd());
        testType(100,new MMax());
        testType(98,new MMax());
    }

    static void testType(int size,Mergers<Integer> mergers){
        Integer[] arr = ArrayGenerator.generateRandomArray(size,size);
        SQRT<Integer> sqrt = new SQRT<>(arr, mergers);

        Object[] a = sqrt.getData();
        for (int i=0;i<arr.length;i++){
            if(!a[i].equals(arr[i])){
                throw new RuntimeException("SQRT GetData failed.");
            }
        }

        int s = (int)Math.sqrt(size);

        test(sqrt,mergers,arr,0,s);
        test(sqrt,mergers,arr,0,s+1);
        test(sqrt,mergers,arr,s-1,s+s);
        test(sqrt,mergers,arr,s-1,s+s+1);
        test(sqrt,mergers,arr,0,s+s+s);
        test(sqrt,mergers,arr,0,s+s+s+1);
    }

    static void test(SQRT<Integer> sqrt,Mergers<Integer> mergers,Integer[] arr,int l,int r){
        int sum = 0;
        Integer[] a = new Integer[r-l+1];
        int j = 0;
        for (int i=0;i<arr.length;i++){
            if (i>=l&&i<=r){
                a[j] = arr[i];
                j++;
            }
        }
        sum = mergers.merges(a);
        System.out.println(sum+":"+sqrt.range(l,r));
    }
}
